package com.hxwm.hwdbtool.util;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HwUtil {
    /**
     * 对象判空
     * @param obj
     * @return
     */
    public static boolean isNullOrEmpty(Object obj){
        if(obj instanceof  Object[]){
            Object[] o = (Object[])((Object[])obj);
            return o == null || o.length ==0;
        }else if(obj instanceof String){
            return obj == null || "".equals(((String)obj).trim());
        }else if(obj instanceof List){
            List objList = (List)obj;
            return objList == null ||objList.isEmpty();
        }else if(obj instanceof Map){
            Map objMap = (Map)obj;
            return objMap == null ||objMap.isEmpty();
        }else{
            return obj == null || "".equals(obj);
        }
    }

    /**
     * 使用正则表达式匹配字符串inputStr是否包含 "regexStr"，不区分大小写
     * @param regexStr
     * @param inputStr
     * @return 如果字符串中包含 "where"，则返回 true，否则返回 false
     */
    public static boolean containsIgnoreCase(String regexStr,String inputStr) {
        if (inputStr == null || regexStr == null) {
            return false;
        }
        // 使用正则表达式进行匹配，不区分大小写
        Pattern pattern = Pattern.compile(regexStr, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(inputStr);
        // 执行匹配
        return matcher.find();
    }

    /**
     * 匹配单个单词前缀
     * @param regexStr
     * @param inputStr
     * @return
     */
    public static boolean startWithSingleWordIgnoreCase(String regexStr,String inputStr) {
        if (inputStr == null || regexStr == null) {
            return false;
        }
        // 使用正则表达式进行匹配，不区分大小写
        String patternString = "(?s)^\\s*" + regexStr + "\\b.*";
        Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(inputStr);
        // 执行匹配
        return matcher.matches();
    }

    public static String generateGlobalFlow() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String formattedDate = dateFormat.format(new Date());

        Random random = new Random();
        int randomNumber = random.nextInt(900) + 100; // 生成一个三位数的随机数

        return "QY100100" + formattedDate + randomNumber;
    }

    /**
     * 解析 (key,value)(key,value)(key,value)字符串
     * @param enumStr
     * @return
     */
    public static Map<Object, Object> enumMapper(String enumStr){
        // 创建一个 Map 存储解析的结果
        Map<Object, Object> enumMap = new HashMap<>();
        if(enumStr!=null && !"".equals(enumStr)){//枚举映射，放在新表
            // 使用正则表达式匹配 key 和 value：(key,value)(key,value)(key,value)
            Pattern pattern = Pattern.compile("\\(([^,]+),([^)]+)\\)");
            Matcher matcher = pattern.matcher(enumStr);
            // 循环匹配并将 key 和 value 放入 Map
            while (matcher.find()) {
                String key = matcher.group(1);
                String value = matcher.group(2);
                enumMap.put(key, value);
            }
        }
        return enumMap;
    }

    /**
     * 解析 WHERE 子句，并将条件放入 Map 中
     * @param whereClause WHERE 子句字符串
     * @return 包含解析结果的 Map
     */
    public static Map<String, String> parseWhereClause(String whereClause) {
        Map<String, String> conditionMap = new HashMap<>();

        // 使用正则表达式匹配 WHERE 子句中的条件
        Pattern pattern = Pattern.compile("(\\w+)\\.(\\w+)\\s*=\\s*(\\w+)\\.(\\w+)");
        Matcher matcher = pattern.matcher(whereClause);

        // 找到匹配项并将结果放入 Map 中
        while (matcher.find()) {
            //String key = matcher.group(1) + "." + matcher.group(2);
            String key = matcher.group(2);
            //String value = matcher.group(3) + "." + matcher.group(4);
            String value = matcher.group(4);
            conditionMap.put(key, value);
        }

        return conditionMap;
    }

    /**
     *
     * @param concatStr
     * @return
     */
    public static List<String> getConcatStr(String concatStr){
        List<String> concatList = new ArrayList<>();
        if(concatStr != null && !"".equals(concatStr)){
            concatStr = concatStr.replace("\\(","");
            concatStr = concatStr.replace("\\)","");
            String[] split = concatStr.split(",");
            List<String> list = Arrays.asList(split);
            concatList.addAll(list);
        }
        return concatList;
    }

    /**
     * 打印指定字符和指定个数的字符
     * @param size
     * @param totalSize
     * @return
     */
    public static String getPrintStr(long size,long totalSize){
        String result = ">";
        double process = ((double) size / (double) totalSize) * 100;
        for (int i = 0; i < (int) Math.round(process) / 5; i++) {
            result = result + ">";
        }
        return result + String.format("%.2f",process) + "%";
    }

    /**
     * 将毫秒转换为客观的时间格式
     * @param mill
     * @return
     */
    public static String transMillisToTime(long mill){
        if(mill < 1000){
            return mill + "ms";
        }else if (mill>=1000 && mill < 60000){
            return mill/1000 + "s";
        }else{
            return mill/1000/60 + "min";
        }
    }
}
